﻿using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;

namespace BadOCR.Runtime
{
    public class LanguageHelper
    {
        /// <summary>
        /// 语种检测(目前使用谷歌来检测, 之后可以换)
        /// </summary>
        /// <returns></returns>
        public static LanguageKind LanguageDetect(string text)
        {
            var request = (HttpWebRequest)WebRequest.Create($"https://translate.googleapis.com/translate_a/single?client=gtx&dt=t&sl=auto&tl=zh-CN&q={text}");
            request.Method = "GET";
            request.ContentType = "application/json; charset=UTF-8";

            var response = (HttpWebResponse)request.GetResponse();
            using (Stream myResponseStream = response.GetResponseStream())
            {
                StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.UTF8);
                var googleLanguageCode = JArray.Parse(myStreamReader.ReadToEnd())[2]?.ToString();
                switch (googleLanguageCode)
                {
                    case "zh-CN":
                        return LanguageKind.SimplifiedChinese;
                    case "en":
                        return LanguageKind.English;
                    case "ja":
                        return LanguageKind.Japanese;
                    case "ko":
                        return LanguageKind.Korean;
                }

                return LanguageKind.Other;
            }
        }

        /// <summary>
        /// 翻译规则如下:
        /// 非母语 -> 母语
        /// 母语  ->  第一外语
        /// </summary>
        /// <param name="sourceLanguage">支持任意语言, 自动检测</param>
        /// <param name="localLanguage">母语</param>
        /// <param name="firstForeignLanguage">第一外语</param>
        /// <returns></returns>
        public static LanguageKind GetTargetLanguage(LanguageKind sourceLanguage,
                                              LanguageKind localLanguage = LanguageKind.SimplifiedChinese,
                                              LanguageKind firstForeignLanguage = LanguageKind.English)
        {
            if (sourceLanguage != localLanguage)
            {
                return localLanguage;
            }
            else
            {
                return firstForeignLanguage;
            }
        }

        #region 语言Code转换
        /// <summary>
        /// https://cloud.baidu.com/doc/OCR/s/zk3h7xz52
        /// </summary>
        /// <param name="languageKind"></param>
        /// <returns></returns>
        public static string GetBaiduLanguageCode(LanguageKind languageKind)
        {
            switch (languageKind)
            {
                case LanguageKind.SimplifiedChinese:
                    return "CHN_ENG";
                case LanguageKind.Japanese:
                    return "JAP";
                case LanguageKind.Korean:
                    return "KOR";
                case LanguageKind.English:
                    return "ENG";
            }

            return string.Empty;
        }

        /// <summary>
        /// http://ocr.space/OCRAPI
        /// </summary>
        /// <param name="languageKind"></param>
        /// <returns></returns>
        public static string GetSpaceOCRLanguageCode(LanguageKind languageKind)
        {
            switch (languageKind)
            {
                case LanguageKind.SimplifiedChinese:
                    return "chs";
                case LanguageKind.Japanese:
                    return "jpn";
                case LanguageKind.Korean:
                    return "kor";
                case LanguageKind.English:
                    return "eng";
            }

            return string.Empty;
        }


        #endregion
    }
}
